package com.erb.service.zwj.impl;

import com.erb.entity.Pager;
import com.erb.entity.Product;
import com.erb.entity.ProductExtends;
import com.erb.mapper.ProductMapper;
import com.erb.service.zwj.IProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("productService")
public class ProductServiceImpl implements IProductService {
	@Resource
	private ProductMapper productMapper;

	/**
	 * 查询所有
	 *
	 * @param pager
	 * @param productExtends
	 * @return
	 */
	@Override
	public List<Product> findAll(Pager pager, ProductExtends productExtends) {
		return productMapper.selectAll(pager, productExtends);
	}

	/**
	 * 查询所有，id降序
	 *
	 * @param pager
	 * @param productExtends
	 * @return
	 */
	@Override
	public List<Product> findAllForList(Pager pager, ProductExtends productExtends) {
		productExtends.setIdDesc(true);// id降序
		return productMapper.selectAll(pager, productExtends);
	}

	/**
	 * 查询所有，id降序，不查询所有列
	 *
	 * @param pager
	 * @param productExtends
	 * @return
	 */
	@Override
	public List<Product> findAllForSimpleList(Pager pager, ProductExtends productExtends) {
		productExtends.setIdDesc(true);// id降序
		productExtends.setAllColumn(false);// 不查询所有列
		return productMapper.selectAll(pager, productExtends);
	}

	@Override
	public int findCount(ProductExtends productExtends) {
		return productMapper.findCount(productExtends);
	}

	@Override
	public Product findProductById(Integer id) {
		Assert.notNull(id, "参数ID不能为空（null）。");

		return productMapper.selectProductById(id);
	}

	@Override
	public boolean saveProduct(Product product) {
		Assert.notNull(product, "参数product不能为空（null）。");

		return productMapper.insertProduct(product) > 0;
	}

	@Override
	public boolean modifyProduct(Product product) {
		Assert.notNull(product, "参数product不能为空（null）！");

		return productMapper.updateProduct(product) > 0;
	}

	@Override
	public boolean modifyProductIncrHaveMoney(int pid, double haveMoney) {
		if (pid <= 0)
			return false;

		return productMapper.updateProductIncrHaveMoney(pid, haveMoney) > 0;
	}

	@Override
	public boolean deleteProduct(Integer id) {
		Assert.notNull(id, "参数ID不能为（NUll）！");

		return productMapper.deleteProduct(id) > 0;
	}
}
